package de.hsos.bose.skillshare.model.core.table;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import de.hsos.bose.skillshare.model.core.DataAccess;
import de.hsos.bose.skillshare.model.objects.SkillDetail;

public class DetailTableModel extends AbstractTableModel {

	private static final long serialVersionUID = 1L;

	private static final String resDetailName = "Detail";
	private static final String resDetailDescription = "Description";

	public static final int DETAIL_NAME = 0;
	public static final int DETAIL_DESCRIPTION = 1;

	protected String[] columnNames = { resDetailName, resDetailDescription };
	protected Vector<SkillDetail> dataVector;

	public DetailTableModel() {
		this.dataVector = new Vector<SkillDetail>();
	}

	public List<SkillDetail> getDirty() {
		List<SkillDetail> dirtyList = new ArrayList<SkillDetail>();
		for (SkillDetail group : dataVector) {
			if (group.isDirty()) {
				dirtyList.add(group);
			}
		}
		return dirtyList;
	}
	
	public List<SkillDetail> getNew(){
		List<SkillDetail> newList = new ArrayList<SkillDetail>();
		for (SkillDetail detail : dataVector) {
			if (detail.isNew()){
				newList.add(detail);
			}
		}
		return newList;
	}
	
	public void addEmptyRow(int groupId){
		SkillDetail newDetail = new SkillDetail();
		newDetail.setNew(true);
		newDetail.setSkillGruppenID(groupId);
		dataVector.add(newDetail);
		fireTableRowsInserted(dataVector.size(), dataVector.size());
	}

	public void loadEmpoyees(int sGroupID) {
		while (dataVector.size() > 0) {
			dataVector.remove(0);
			fireTableRowsDeleted(0, 0);
		}
		DataAccess da = new DataAccess();
		List<SkillDetail> detailList = da.getSkillDetails(sGroupID);

		for (SkillDetail detail : detailList) {
			dataVector.add(detail);
			fireTableRowsInserted(dataVector.size(), dataVector.size());
		}
	}

	public SkillDetail getSkillDetail(int index) {
		return dataVector.get(index);
	}

	@Override
	public String getColumnName(int column) {
		return columnNames[column];
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return true;
	}

	public Class<?> getColumnClass(int columnIndex) {
		switch (columnIndex) {
		case DETAIL_NAME:
		case DETAIL_DESCRIPTION:
			return String.class;
		default:
			return Object.class;
		}
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		SkillDetail record = (SkillDetail) dataVector.get(rowIndex);
		switch (columnIndex) {
		case DETAIL_NAME:
			return record.getSkillDetailName();
		case DETAIL_DESCRIPTION:
			return record.getSkillDetailBeschreibung();
		default:
			return new Object();
		}
	}

	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		SkillDetail record = (SkillDetail) dataVector.get(rowIndex);
		switch (columnIndex) {
		case DETAIL_NAME:
			record.setSkillDetailName((String) aValue);
			break;
		case DETAIL_DESCRIPTION:
			record.setSkillDetailBeschreibung((String) aValue);
			break;
		default:
			System.out.println("invalid index");
		}
		fireTableCellUpdated(rowIndex, columnIndex);
	}

	@Override
	public int getColumnCount() {
		return columnNames.length;
	}

	@Override
	public int getRowCount() {
		return dataVector.size();
	}

}
